import { Module, ValidationPipe } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CoffeeModule } from './modules/coffee/coffee.module';
import { RatingModule } from './modules/rating/rating.module';
import { DatabaseModule } from './modules/database/database.module';
import { ConfigModule } from '@nestjs/config';
import * as Joi from '@hapi/joi';
import appConfig from './config/app.config';
import { APP_PIPE } from '@nestjs/core';
import { CommonModule } from './modules/common/common.module';
import { PostsModule } from './modules/posts/posts.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      // ConfigModule 会在程序中找.env文件，你可以通过以下方式来指定另一个路径
      // envFilePath: 'environment', // [environment1, environment2]
      // 禁止加载.env文件
      // ignoreEnvFile: true,
      load: [appConfig],
      validationSchema: Joi.object({
        DATABASE_HOST: Joi.required(),
        DATABASE_PORT: Joi.number().default(5432),
      }),
    }),
    CoffeeModule,
    TypeOrmModule.forRootAsync({
      useFactory: () => ({
        type: 'postgres',
        host: process.env.DATABASE_HOST,
        port: +process.env.DATABASE_PORT,
        username: process.env.DATABASE_USER,
        password: process.env.DATABASE_PASSWORD,
        database: process.env.DATABASE_NAME,
        autoLoadEntities: true,
        synchronize: true,
      }),
    }),
    RatingModule,
    CommonModule,
    PostsModule,
    // DatabaseModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    // 全局使用管道，局部使用管道参考coffee.controller.ts 的 @usePipes()装饰器
    /* {
      provide: APP_PIPE,
      useClass: ValidationPipe,
    }, */
  ],
})
export class AppModule {}
